import {
  getChannelList,
  createChannel,
  detailsChannel,
  updateChannel,
  updateChannelState,
  auditChannel,
  getProductDiviList,
  channelProductOfdetailsUpdata,
  deleteChannelFile,
} from '../services/api';
import { message } from 'antd';
import { isResponerCode } from '../utils/utils';
export default {
  namespace: 'channel',

  state: {
    channelList: [],
    channelPage: {},
    channelDetailsLoading: false,
    channelLoading: false,
    channelDetailValue: null,
    channelResult: {},
    channelDetails: null,
    updateChannelResult: {},
    channelDetailsId: null,
    channelProductDiviList: null,
    channelUpdateProductDiviList: null,
    channelProductLoading: false,
  },

  effects: {
    *fetchChannel({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          channelLoading: true,
        },
      });
      const response = yield call(getChannelList, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            channelLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'show',
        payload: {
          channelLoading: false,
        },
      });
      yield put({
        type: 'show',
        payload: {
          channelList: response.data.list,
          channelPage: response.data.page,
        },
      });
      if (callback) callback();
    },
    //新增渠道
    *createChannel({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          channelLoading: true,
        },
      });
      const response = yield call(createChannel, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            channelLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'show',
        payload: {
          channelLoading: false,
        },
      });
      yield put({
        type: 'show',
        payload: {
          data: response.data,
          channelResult: response.result,
        },
      });
      if (callback) callback();
    },
    //修改渠道
    *updateChannel({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          channelLoading: true,
        },
      });
      const response = yield call(updateChannel, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            channelLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'show',
        payload: {
          channelLoading: false,
        },
      });
      yield put({
        type: 'show',
        payload: {
          channelDetailsId: response.data,
          updateChannelResult: response.result,
        },
      });
      if (callback) callback();
    },
    //获取渠道详情
    *detailsChannel({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          channelLoading: true,
        },
      });
      const response = yield call(detailsChannel, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            channelLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'show',
        payload: {
          channelLoading: false,
        },
      });
      yield put({
        type: 'show',
        payload: {
          channelDetails: response.data,
          channelUpdateProductDiviList: response.data.productList,
        },
      });
      if (callback) callback();
    },
    //修改渠道状态
    *updateChannelState({ payload, callback }, { call, put }) {
      const response = yield call(updateChannelState, payload);
      if (response.result.code == '200') {
        message.success('修改成功');
      } else {
        message.success(response.result.message);
      }
      yield put({
        type: 'show',
        payload: {
          data: response.data,
        },
      });
      if (callback) callback();
    },
    //渠道审核
    *auditChannel({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          channelLoading: true,
        },
      });
      const response = yield call(auditChannel, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            channelLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'show',
        payload: {
          channelLoading: false,
        },
      });
      yield put({
        type: 'show',
        payload: {
          data: response.data,
          updateChannelResult: response.result,
        },
      });
      if (callback) callback();
    },

    //获取产品列表及分期列表--新增
    *getProductDiviList({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          channelLoading: true,
        },
      });
      const response = yield call(getProductDiviList, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            channelLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'show',
        payload: {
          channelLoading: false,
        },
      });
      yield put({
        type: 'show',
        payload: {
          channelProductDiviList: response.data,
        },
      });
      if (callback) callback();
    },

    //删除渠道附件---- 渠道
    *deleteFile({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          channelLoading: true,
        },
      });
      const response = yield call(deleteChannelFile, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            channelLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'show',
        payload: {
          channelLoading: false,
        },
      });
      yield put({
        type: 'show',
        payload: {
          data: response.data,
          channelResult: response.result
        },
      });
      if (callback) callback();
    },
    //渠道修改
    *changeUpdateProduct({ payload, callback }, { select, call, put }) {
      yield put({
        type: 'saveNewCheckStatus',
        payload,
      });
      if (callback) callback();
    },
    //渠道新增
    *changeAddProduct({ payload, callback }, { select, call, put }) {
      yield put({
        type: 'saveAddProduct',
        payload,
      });
      if (callback) callback();
    },
  },

  reducers: {
    show(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    setChannelValue(state, action) {
      return {
        ...state,
        channelDetailValue: action.payload,
      };
    },
    //编辑
    saveNewCheckStatus(state, { payload }) {
      const { item } = payload;
      const channelUpdateProductDiviList = state.channelDetails.productList;
      const newStructureProductDiviList = channelUpdateProductDiviList.map(
        oldItem => (oldItem.id == item.id ? item : oldItem)
      );
      return {
        ...state,
        channelUpdateProductDiviList: newStructureProductDiviList,
      };
    },
    /*
    *渠道新增和编辑上面👆下面👇两个方法不可合并，新增会调代理权限接口，编辑是得到详情里面的代理权限的数据
    */
    //新增
    saveAddProduct(state, { payload }) {
      const { item } = payload;
      const channelProductDiviList = state.channelProductDiviList;
      const newStructureProductDiviList = channelProductDiviList.map(
        oldItem => (oldItem.id == item.id ? item : oldItem)
      );
      return {
        ...state,
        channelProductDiviList: newStructureProductDiviList,
      };
    },
  },
};
